#include <AddrLinkedList.h>

Node::Node()
{
    addr = 0;
    next = 0;
}

AddrLinkedList::AddrLinkedList()
{
    head = new Node;
    tail = head;
    count = 0;
}

AddrLinkedList::~AddrLinkedList()
{
    clear();
    delete head;
}


//增加一个结点
void AddrLinkedList::push(void* addr)
{
    Node* node = new Node;
    node->addr = addr;
    tail->next = node;
    tail = node;
    count++;
    if(head->next == 0)
    {
        head->next = node;
    }
}

//清空所有
void AddrLinkedList::clear()
{
    for(Node* i = head->next; i != 0; )
    {
        Node* temp = i;
        i = i->next;
        delete temp;
    }
    head->next = 0;
    tail = head;
    count = 0;
}

//弹出最后一个
void* AddrLinkedList::pop()
{
    if(isEmpty())
    {
        return 0;
    }
    
    for(Node* i = head; i->next != 0; i = i->next)
    {
        if(i->next == tail)
        {
            void* temp = tail->addr;
            delete i->next;
            i->next = 0;
            tail = i;
            count--;
            return temp;
        }
    }
    return 0;
}

//是否为空
bool AddrLinkedList::isEmpty()
{
    return head == tail;
}


//结点个数
int AddrLinkedList::size()
{
	return this->count;
}

void AddrLinkedList::remove(void* addr)
{
    for(Node* i = head; i->next != 0; i = i->next)
    {
        if(i->next->addr == addr)
        {
            Node* temp = i->next;
            i->next = i->next->next;
            delete temp;
            count--;
            break;
        }
    }
}

//返回最后一个结点的内容
void* AddrLinkedList::top()
{
	if(isEmpty())
	{
		return 0;
	}
	
	return tail->addr;
}

